<?php
class Staff_db extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function is_stafftype_unique($stafftype) {
        $where = "(type_label='".$stafftype."')"; 
        $this->db->where($where);
        $result = $this->db->count_all_results('stafftype');
        if($result>0)
            return false;
        else
            return true; 
    }
    
    function save_stafftype($data,$pk='') {
        $save_result = array();
        
        if ($pk=='') {
    		$this->db->insert('stafftype', $data);
    		$save_result['insert_id'] = $this->db->insert_id();
            if($save_result['insert_id'] == 0 || $save_result['insert_id'] == false) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		} else {
    		$this->db->where('type_id', $pk);
    		$this->db->update('stafftype', $data);
            //print_object($this->db->last_query());exit;
            $save_result['affected_rows'] = $this->db->affected_rows();
            if($save_result['affected_rows'] == -1 ) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		}   
        return $save_result;
    }
    
    function save_staff($data,$pk='') {
        $save_result = array();
        
        if ($pk=='') {
    		$this->db->insert('staff', $data);
    		$save_result['insert_id'] = $this->db->insert_id();
            if($save_result['insert_id'] == 0 || $save_result['insert_id'] == false) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		} else {
    		$this->db->where('type_id', $pk);
    		$this->db->update('staff', $data);
            //print_object($this->db->last_query());exit;
            $save_result['affected_rows'] = $this->db->affected_rows();
            if($save_result['affected_rows'] == -1 ) {
                $save_result['result'] = 'F';
                $save_result['error_no'] = $this->db->_error_number();
                $save_result['error_message'] = $this->db->_error_message();
            } else {
                $save_result['result'] = 'S';
            }
		}   
        return $save_result;
    }
    
    function get_stafftypes($formated=false) {
        $query = $this->db->get('stafftype');
        $result = $query->result_array();
        if($formated) {
            $result = format_list($result, "type_id", "type_label");
        }
        return $result;
    }
    
    function get_stafflist($staff_type = '') {
        if($staff_type != '')
        $this->db->where('staff_type_id', $staff_type);
        
        $query = $this->db->get('staff');
        $result = $query->result_array();
        return $result;
    }
    
}

?>